package com.kgc.kmall.managerservice.service.impl;

import com.kgc.kmall.bean.*;
import com.kgc.kmall.managerservice.mapper.*;
import com.kgc.kmall.service.SpuService;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SpuServiceImpl implements SpuService {
    @Resource
    PmsProductInfoMapper pmsProductInfoMapper;
    @Resource
    PmsBaseSaleAttrMapper pmsBaseSaleAttrMapper;
    @Resource
    PmsProductImageMapper pmsProductImageMapper;
    @Resource
    PmsProductSaleAttrMapper pmsProductSaleAttrMapper;
    @Resource
    PmsProductSaleAttrValueMapper pmsProductSaleAttrValueMapper;


    @Override
    public List<PmsProductInfo> spuList(Long catalog3Id) {
        PmsProductInfoExample example=new PmsProductInfoExample();
        PmsProductInfoExample.Criteria criteria=example.createCriteria();
        criteria.andCatalog3IdEqualTo(catalog3Id);
        List<PmsProductInfo> pmsProductInfos = pmsProductInfoMapper.selectByExample(example);
        return pmsProductInfos;
    }

    @Override
    public List<PmsBaseSaleAttr> baseSaleAttrList() {
        return pmsBaseSaleAttrMapper.selectByExample(null);
    }

    @Override
    public int saveSpuInfo(PmsProductInfo pmsProductInfo) {
        int i=0;
        if (pmsProductInfo.getId()==null){
            i= pmsProductInfoMapper.insert(pmsProductInfo);
        }else{

        }
        //添加图片
        List<PmsProductImage> spuImageList = pmsProductInfo.getSpuImageList();
        if (spuImageList.size()>0){
           i= pmsProductImageMapper.insertBatch(pmsProductInfo.getId(),pmsProductInfo.getSpuImageList());
        }
        //添加属性
        List<PmsProductSaleAttr> spuSaleAttrList = pmsProductInfo.getSpuSaleAttrList();
            if (spuSaleAttrList.size()>0){
                i=pmsProductSaleAttrMapper.insertBatch(pmsProductInfo.getId(),pmsProductInfo.getSpuSaleAttrList());
                for (PmsProductSaleAttr productSaleAttr : pmsProductInfo.getSpuSaleAttrList()) {
                    if (productSaleAttr.getSpuSaleAttrValueList().size()>0){
                            pmsProductSaleAttrValueMapper.insertBatch(pmsProductInfo.getId(),productSaleAttr.getSpuSaleAttrValueList());
                    }
                }
            }

        return i;
    }

    @Override
    public List<PmsProductSaleAttr> spuSaleAttrList(Long spuId) {
        PmsProductSaleAttrExample example=new PmsProductSaleAttrExample();
        PmsProductSaleAttrExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(spuId);
        List<PmsProductSaleAttr> pmsProductSaleAttrs = pmsProductSaleAttrMapper.selectByExample(example);
        for (PmsProductSaleAttr pmsProductSaleAttr : pmsProductSaleAttrs) {
            PmsProductSaleAttrValueExample valueExample=new PmsProductSaleAttrValueExample();
            PmsProductSaleAttrValueExample.Criteria criteria1 = valueExample.createCriteria();
            criteria1.andProductIdEqualTo(spuId);
            criteria1.andSaleAttrIdEqualTo(pmsProductSaleAttr.getSaleAttrId());
            List<PmsProductSaleAttrValue> pmsProductSaleAttrValues = pmsProductSaleAttrValueMapper.selectByExample(valueExample);
        pmsProductSaleAttr.setSpuSaleAttrValueList(pmsProductSaleAttrValues);
        }
        return pmsProductSaleAttrs;
    }

    @Override
    public List<PmsProductImage> spuImageList(Long spuId) {
        List<PmsProductImage> pmsProductImages=null;



        PmsProductImageExample example=new PmsProductImageExample();
        PmsProductImageExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(spuId);
        pmsProductImages = pmsProductImageMapper.selectByExample(example);
        return pmsProductImages;
    }

    @Override
    public List<PmsProductSaleAttr> spuSaleAttrListIsCheck(Long spuId, Long skuId) {
        List<PmsProductSaleAttr> pmsProductSaleAttrList = pmsProductSaleAttrMapper.spuSaleAttrListIsCheck(spuId, skuId);
        return pmsProductSaleAttrList;
    }
}
